package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.baidu.location.LocationClient;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ShopsAdapter;
import com.iexbuy.ihg.adapter.ViewPageAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.AdObject;
import com.iexbuy.ihg.entity.BusinessType;
import com.iexbuy.ihg.entity.BusinessTypeResponse;
import com.iexbuy.ihg.entity.CityObject;
import com.iexbuy.ihg.entity.CityObjectResponce;
import com.iexbuy.ihg.entity.DistrictObject;
import com.iexbuy.ihg.entity.DistrictObjectResponse;
import com.iexbuy.ihg.entity.Shop;
import com.iexbuy.ihg.entity.ShopCenter;
import com.iexbuy.ihg.entity.ShopCenterListResponse;
import com.iexbuy.ihg.entity.ShopCenterResponse;
import com.iexbuy.ihg.entity.ShopListResponse;
import com.iexbuy.ihg.entity.YunItemListObject;
import com.iexbuy.ihg.entity.YunItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.ui.activity.MainTabActivity;
import com.iexbuy.ihg.ui.view.FixGridLayout;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class HomeFragment extends BaseFragment {

    private static HomeFragment homeFragmentInstent;

    @ViewInject(R.id.msgListLayout)
    private PullToRefreshLayout msgListRefreshLayout;
    @ViewInject(R.id.titleLayout)
    private RelativeLayout titleLayout;
    @ViewInject(R.id.listView)
    private ListView listView;
    @ViewInject(R.id.mSearchBtn)
    private EditText mSearchBtn;
    @ViewInject(R.id.city)
    private TextView city;
    @ViewInject(R.id.currentCity)
    private TextView currentCity;

    private LinearLayout listHeaderView;
    //	@ViewInject(R.id.adItemList)
//	private ViewPager adItemList;
    @ViewInject(R.id.adItemList)
    private RelativeLayout adItemList;
    @ViewInject(R.id.ad0GList)
    private RelativeLayout ad0GList;
    @ViewInject(R.id.viewpagerCategory)
    private ViewPager viewpagerCategory;
    @ViewInject(R.id.zeroGuoLastThree)
    private LinearLayout zeroGuoLastThree;
    @ViewInject(R.id.distictLayout)
    private GridView distictLayout;
    @ViewInject(R.id.cityLayout)
    private LinearLayout cityLayout;
    //购物中心模块
    @ViewInject(R.id.shopCenterLayout)
    private LinearLayout shopCenterLayout;
    //购物中心名称

    @ViewInject(R.id.shopCenterNameTvOne)
    private TextView shopCenterNameTvOne;
    @ViewInject(R.id.shopCenterNameTvTwo)
    private TextView shopCenterNameTvTwo;
    @ViewInject(R.id.shopCenterNameTvThree)
    private TextView shopCenterNameTvThree;
    //优惠商家数量
    @ViewInject(R.id.recommendCountTwo)
    private TextView recommendCountTwo;
    @ViewInject(R.id.recommendCountThree)
    private TextView recommendCountThree;

    //全部商家数量
    @ViewInject(R.id.shoppingCenterCount)
    private TextView shoppingCenterCount;
    //
    @ViewInject(R.id.shopCenterNameImgOne)
    private ImageView shopCenterNameImgOne;
    @ViewInject(R.id.shopCenterNameImgTwo)
    private ImageView shopCenterNameImgTwo;
    @ViewInject(R.id.shopCenterNameImgThree)
    private ImageView shopCenterNameImgThree;

    private List<AdObject> adObjects = new ArrayList<AdObject>();
    private List<View> adItemViews = new ArrayList<View>();
    private List<ShopCenter> shopCenterListObjects = new ArrayList<ShopCenter>();
    private List<Shop> shops = new ArrayList<Shop>();

    private int pageNo = 0;
    private ShopsAdapter shopsAdapter;
    private boolean hasNextPage;

    //底部小店图片
    private ImageView[] dots;
    private List<View> views;
    private int currentIndex;
    //云购
    private List<YunItemListObject> yunItemListObjects = new ArrayList<YunItemListObject>();

    //热门分类
    private static List<BusinessType> businessTypes = new ArrayList<BusinessType>();
    private List<View> businessTypePageViews = new ArrayList<View>();

    //购物中心
    private static List<ShopCenter> ShopCentersTop = new ArrayList<ShopCenter>();

    private static CityObject selectCityObject = new CityObject("广州", "440100");
    private static DistrictObject selectDistrictObject;

    private static List<DistrictObject> districtObjects = new ArrayList<DistrictObject>();
    private List<CityObject> cityObjects = new ArrayList<CityObject>();
    private int [] icoImgs1  ={R.drawable.icon_category_1,R.drawable.icon_category_2,R.drawable.icon_category_3,R.drawable.icon_category_4,R.drawable.icon_category_5,R.drawable.icon_category_6,R.drawable.icon_category_7,R.drawable.icon_category_8};
    private int [] icoImgs2  ={R.drawable.icon_category_9,R.drawable.icon_category_10,R.drawable.icon_category_11,R.drawable.icon_category_12,R.drawable.icon_category_13,R.drawable.icon_category_14,R.drawable.icon_category_15,R.drawable.icon_category_16};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        homeFragmentInstent = this;
    }

    @Override
    protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        ViewUtils.inject(this, rootView);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        mBaseActivity.setTitleLayoutVisibility(false);
        mBaseActivity.setHomeActivity(true);
        mBaseActivity.setSlideRightBackEnable(false);

        listHeaderView = (LinearLayout) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_home_header, null);
        ViewUtils.inject(this, listHeaderView);

        listView.addHeaderView(listHeaderView);

        msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                App.mLocationClient.start();
                refreshDate();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                loadMore();
            }
        });
        msgListRefreshLayout.autoRefresh();

        city.setText(App.sPreferences.getCityName());

        initCityView();
        initCityListData();
    }

    //刷新整个页面数据
    private void refreshDate() {
        pageNo = 0;
        initListDate();

    }

    //获取热门分类
    private void initHotCategoryData() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        ServiceHelper.send(mBaseActivity, ServiceHelper.GET_BUSINESS_HOT_TYPE, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
                    }

                    @Override
                    public void onCancelled() {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        BusinessTypeResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, BusinessTypeResponse.class);
                        businessTypes.clear();
                        businessTypes.addAll(rsp.getBody());
                        initBusinessTypeView();
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                        ToastUtils.toast(mBaseActivity, msg);
                    }
                });
    }

    private void initBusinessTypeView() {
        //分页
        businessTypePageViews.clear();
        List<List<BusinessType>> businessTypePages = new ArrayList<List<BusinessType>>();
        List<BusinessType> businessTypeSubs = new ArrayList<BusinessType>();
        for (int i = 0; i < businessTypes.size(); i++) {
            for (int j = 0;j<businessTypes.size();j++){
                if (businessTypes.get(j).getCode().equals((i+1)+"")) {
                    businessTypeSubs.add(businessTypes.get(j));
                    if (businessTypeSubs.size()==8) {
                        businessTypePages.add(businessTypeSubs);

                        if (businessTypePages.size()==1) {
                        businessTypeSubs = new ArrayList<BusinessType>();
                        }
                    }
                break;
                }
            }
        }

        //初始化每一页grid
        GridView gridView;
        List<BusinessType> businessTypeSubList;
        int [] icoImg;
        for (int i = 0; i < businessTypePages.size(); i++) {
            if (i==0){
                icoImg =icoImgs1;
            }else {
                icoImg =icoImgs2;
            }
            final int [] icoImgs = icoImg;
            businessTypeSubList=businessTypePages.get(i);
            final List<BusinessType> businessTypeSubLists = businessTypeSubList;

            gridView = (GridView) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_home_header_grid, null);
            gridView.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return businessTypeSubLists.size();
                }

                @Override
                public Object getItem(int i) {
                    return businessTypeSubLists.get(i);
                }

                @Override
                public long getItemId(int i) {
                    return Long.valueOf(businessTypeSubLists.get(i).getCode());
                }

                @Override
                public View getView(int i, View view, ViewGroup viewGroup) {
                    if (view == null) {
                        view = mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_home_header_grid_subview, null);
                    }
                    view.setTag(businessTypeSubLists.get(i));
                    ((TextView) view.findViewById(R.id.ItemText)).setText(businessTypeSubLists.get(i).getName());
                    ImageView iv =(ImageView) view.findViewById(R.id.ItemImage);
                    iv.setImageResource(icoImgs[i]);
                   // App.bitmapUtils.display((ImageView) view.findViewById(R.id.ItemImage), businessTypeSubLists.get(i).getThumbnailUrl());
                    return view;
                }
            });
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    XiaoFeiSearchResultFragment.setSelectedBusinessTypeCode(((BusinessType) view.getTag()).getCode());
                    XiaoFeiSearchResultFragment.setSelectedBusinessTypeName(((BusinessType) view.getTag()).getName());
                    CommUtil.openSingleFragmentActivity(mBaseActivity, XiaoFeiSearchResultFragment.class);
                }
            });
            businessTypePageViews.add(gridView);

        }

        //圆点图片添加
        dots = new ImageView[businessTypePages.size()];
        initDots();

        //初始viewPage
        viewpagerCategory.setAdapter(new ViewPageAdapter(businessTypePageViews));
        viewpagerCategory.getAdapter().notifyDataSetChanged();
        viewpagerCategory.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                                      @Override
                                                      public void onPageScrolled(int i, float v, int i1) {

                                                      }

                                                      @Override
                                                      public void onPageSelected(int i) {
                                                          setCurDot(i);
                                                      }

                                                      @Override
                                                      public void onPageScrollStateChanged(int i) {

                                                      }
                                                  }

        );

    }

    /**
     * 初始化圆点
     */
    private void initDots() {
        views = new ArrayList<View>();
        LinearLayout ll_dots = (LinearLayout) listHeaderView.findViewById(R.id.ll_dots);

        for (int i = 0; i < dots.length; i++) {
            dots[i] = (ImageView) ll_dots.getChildAt(i);
            dots[i].setEnabled(true);//都设为红色
        }
        currentIndex = 0;
        dots[currentIndex].setEnabled(false);//设置为白色，即选中状态
    }

    private void setCurDot(int positon) {
        if (positon < 0 || positon > dots.length - 1 || currentIndex == positon) {
            return;
        }
        dots[positon].setEnabled(false);
        dots[currentIndex].setEnabled(true);
        currentIndex = positon;
    }

    //初始云购广告
    private void initYunguoAdListData() {

    }

    //初始云购列表
    private void initYunguoListData() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("pageNo", "0");
        paramMap.put("pageSize", "6");
        ServiceHelper.send(mBaseActivity, ServiceHelper.GET_YUN_GUO_READY_OPEN_ITEMS, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onCancelled() {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        YunItemListObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, YunItemListObjectResponse.class);
                        yunItemListObjects.clear();
                        yunItemListObjects.addAll(rsp.getBody());
                        initYunGuoListView();
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                    }
                });
    }

    /**
     * 0购推荐
     */
    private void initYunGuoListView() {
        zeroGuoLastThree.removeAllViews();
        RelativeLayout yunSubView;
        for (YunItemListObject yunItem : yunItemListObjects) {
            yunSubView = (RelativeLayout) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_home_yun_item_subview, null);
            App.bitmapUtils.display(yunSubView.findViewById(R.id.yungouImg), yunItem.getThumbnailUrl());
            ((TextView) yunSubView.findViewById(R.id.yungouName)).setText(yunItem.getItemName());
            ((TextView) yunSubView.findViewById(R.id.attendanceNo)).setText(yunItem.getAttandanceNumber());
            ((TextView) yunSubView.findViewById(R.id.totalRequireNo)).setText(yunItem.getRequiredTotalNumber());
            ((TextView) yunSubView.findViewById(R.id.balenceNo)).setText(String.valueOf(Integer.valueOf(yunItem.getRequiredTotalNumber()) - Integer.valueOf(yunItem.getAttandanceNumber())));
            ((ProgressBar) yunSubView.findViewById(R.id.yunProgress)).setProgress(new BigDecimal(yunItem.getAttandanceNumber()).divide(new BigDecimal(yunItem.getRequiredTotalNumber()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).intValue());
            yunSubView.setTag(yunItem);
            yunSubView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    YunItemListObject yunItem = (YunItemListObject) view.getTag();
                    YunGuoItemDetailFragment.setYunItemListObject(yunItem);
                    CommUtil.openSingleFragmentActivity(mBaseActivity, YunGuoItemDetailFragment.class);
                }
            });
            zeroGuoLastThree.addView(yunSubView);
        }
    }


    //获取附近商户
    private void initListDate() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("latitude", CommUtil.getLatitude());
        paramMap.put("longitude", CommUtil.getLongitude());

        paramMap.put("pageNo", String.valueOf(pageNo));
        paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));

        if (selectCityObject != null) {
            paramMap.put("cityCode", selectCityObject.getCityCode());
        }
        if (selectDistrictObject != null) {
            paramMap.put("district", selectDistrictObject.getCode());
        }

        ServiceHelper.send(mBaseActivity, ServiceHelper.GET_NEAR_SHOPS, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
                    }

                    @Override
                    public void onCancelled() {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        ShopListResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopListResponse.class);
                        hasNextPage = rsp.getBody().isHasNext();
                        if (pageNo == 0) {
                            shops.clear();
                        }
                        shops.addAll(rsp.getBody().getData());
                        initListView();
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                        ToastUtils.toast(mBaseActivity, msg);
                    }
                });
    }

    private void loadMore() {
        pageNo++;
        initListDate();
    }

    private void initListView() {
        Collections.sort(shops, new Comparator<Shop>() {
            @Override
            public int compare(Shop shop, Shop t1) {
                return new BigDecimal(shop.getDistance()).compareTo(new BigDecimal(t1.getDistance()));
            }
        });
        if (shopsAdapter == null) {
            shopsAdapter = new ShopsAdapter(mBaseActivity, shops);
            listView.setAdapter(shopsAdapter);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    ShopDetailFragment.setShop(shops.get(i - 1));//列表有头部
                    CommUtil.openSingleFragmentActivity(mBaseActivity, ShopDetailFragment.class);
                }
            });
        }
        if (pageNo == 0) {
            //加载列表头部
            CommUtil.initAdListData(mBaseActivity, "1", adItemList);
            initHotCategoryData();
            CommUtil.initAdListData(mBaseActivity, "2", ad0GList);
            initYunguoListData();
            initShopCenterData();
            getAllShopCentersData();
        }
        shopsAdapter.notifyDataSetChanged();
    }

    /**
     * 加载购物中心TOP3家数据
     */
    private void initShopCenterData() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("latitude", CommUtil.getLatitude());
        paramMap.put("longitude", CommUtil.getLongitude());
        paramMap.put("city", selectCityObject.getCityCode());
        ServiceHelper.send(mBaseActivity, ServiceHelper.GET_TOP_SHOP_CENTERS, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
                    }

                    @Override
                    public void onCancelled() {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);

                        ShopCenterResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopCenterResponse.class);
                        ShopCentersTop.clear();
                        ShopCentersTop.addAll(rsp.getBody());
                        Log.d("aa", ShopCentersTop.size() + "");
                        Log.d("aa",ShopCentersTop+"");
                        initShopCentersView();
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);

                    }
                });
    }

    /**
     * 加载全部购物中心的列表数据
     */
    public void getAllShopCentersData() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("latitude", CommUtil.getLatitude());
        paramMap.put("longitude", CommUtil.getLongitude());
        paramMap.put("pageNo", String.valueOf(pageNo));
        paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
        paramMap.put("city", selectCityObject.getCityCode());


        ServiceHelper.send(mBaseActivity, ServiceHelper.GET_SHOP_CENTERS, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onCancelled() {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        ShopCenterListResponse rep = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopCenterListResponse.class);

                        if (pageNo == 0) {
                            shopCenterListObjects.clear();
                        }
                        if (rep.getBody() != null) {
                            shopCenterListObjects.addAll(rep.getBody().getData());
                            shoppingCenterCount.setText("全部" + shopCenterListObjects.size() + "家");

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                    }
                });
    }
    /**
     * 初始化购物中心数据
     */
    private void initShopCentersView() {
        if (ShopCentersTop.size()==0) {

            return;
        }

        for (int i =0;i<ShopCentersTop.size();i++){
            ShopCenter shopCenter = ShopCentersTop.get(i);

            if(i==0){
                shopCenterNameTvOne.setText(shopCenter.getMallName());
                App.bitmapUtils.display(shopCenterNameImgOne, shopCenter.getThumbnailUrl());
                shopCenterNameImgOne.setScaleType(ImageView.ScaleType.FIT_XY);
            }else if(i==1){
                shopCenterNameTvTwo.setText(shopCenter.getMallName());
                recommendCountTwo.setText(shopCenter.getTotalShops()+"家优惠");
                App.bitmapUtils.display(shopCenterNameImgTwo, shopCenter.getThumbnailUrl());
                shopCenterNameImgTwo.setScaleType(ImageView.ScaleType.FIT_XY);
            }else if(i==2){
                shopCenterNameTvThree.setText(shopCenter.getMallName());
                recommendCountThree.setText(shopCenter.getTotalShops()+"家优惠");
                App.bitmapUtils.display(shopCenterNameImgThree, shopCenter.getThumbnailUrl());
                shopCenterNameImgThree.setScaleType(ImageView.ScaleType.FIT_XY);
            }
        }
    }

    /**
     * 进入购物中心列表
     * @param view
     */
    @OnClick(R.id.shoppingCenterLayout)
    public void shoppingCenterLayoutClick(View view) {
        if(ShopCentersTop.size()==0) return;
        ShopCenterSearchResultFragment.setCityCode(selectCityObject.getCityCode());
        ShopCenterSearchResultFragment.setShopCenters(shopCenterListObjects);
        CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterSearchResultFragment.class);
    }
    /**
     * 三个购物中心的按钮接入
     * @param view
     */
    @OnClick(R.id.recommendCenterLayoutOne)
    public void recommendCenterLayoutOneClick(View view) {
        if(ShopCentersTop.size()==0) return;
        ShopCenterDetailFragment.setShopCenter(ShopCentersTop.get(0));
        ShopCenterDetailFragment.setCityShopCenter(selectCityObject);
        ShopCenterDetailFragment.setDistrictShopCenter(selectDistrictObject);
        CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterDetailFragment.class);
    }
    @OnClick(R.id.recommendCenterLayoutTwo)
    public void recommendCenterLayoutTwoClick(View view) {
        if(ShopCentersTop.size()==0) return;
        ShopCenterDetailFragment.setShopCenter(ShopCentersTop.get(1));
        ShopCenterDetailFragment.setCityShopCenter(selectCityObject);
        ShopCenterDetailFragment.setDistrictShopCenter(selectDistrictObject);
        CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterDetailFragment.class);
    }
    @OnClick(R.id.recommendCenterLayoutThree)
    public void recommendCenterLayoutThreeClick(View view) {
        if(ShopCentersTop.size()==0) return;
        ShopCenterDetailFragment.setShopCenter(ShopCentersTop.get(2));
        ShopCenterDetailFragment.setCityShopCenter(selectCityObject);
        ShopCenterDetailFragment.setDistrictShopCenter(selectDistrictObject);
        CommUtil.openSingleFragmentActivity(mBaseActivity, ShopCenterDetailFragment.class);
    }
    @OnClick(R.id.homeShareBtn)
    public void homeShareBtnClick(View view) {
        CommUtil.openSingleFragmentActivity(mBaseActivity, WeDeShareFragment.class);
    }
    @OnClick(R.id.yunGouListLayout)
    public void jumpYunGouClick(View view) {
        MainTabActivity.getInstance().setCurrentTab(2);
    }

    @OnClick(R.id.jumpLingQiang)
    public void jumpLingQiangClick(View view) {
        MainTabActivity.getInstance().setCurrentTab(3);
    }

    @OnClick(R.id.jumpYunguo)//图片显示其实是换缺钱
    public void jumpYunguoClick(View view) {
        MainTabActivity.getInstance().setCurrentTab(1);
    }

    @OnClick(R.id.mSearchBtn)
    public void mSearchBtnClick(View view) {
        CommUtil.openSingleFragmentActivity(mBaseActivity, XiaoFeiSearchFragment.class);
    }

    @OnClick(R.id.cityLayout)
    public void cityLayoutClick(View view) {
        cityLayout.setVisibility(View.GONE);
    }

    @OnClick(R.id.changeCityBtn)
    public void changeCityBtnClick(View view) {
        CityChooseFragment.addOnCitySelectedListener(new CityChooseFragment.onCitySelectedListener() {
            @Override
            public void onCitySelected(CityObject cityObject) {
                selectCityObject = cityObject;
                initCityView();
                initListDate();
            }
        });
        CommUtil.openSingleFragmentActivity(mBaseActivity, CityChooseFragment.class);
        cityLayout.setVisibility(View.GONE);
    }

    @OnClick(R.id.city)
    public void cityClick(View view) {
        if (cityLayout.getVisibility() == View.GONE) {
            cityLayout.setVisibility(View.VISIBLE);
            if (districtObjects.isEmpty()) {
                initCityEreaData();
            }
        } else {
            cityLayout.setVisibility(View.GONE);
        }
    }

    private void initCityEreaData() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("cityCode", selectCityObject.getCityCode());
        ServiceHelper.send(mBaseActivity, ServiceHelper.GET_DISTRICT_BY_CITY_CODE, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onCancelled() {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        DistrictObjectResponse rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, DistrictObjectResponse.class);
                        if (rsp.getBody() != null) {
                            districtObjects = rsp.getBody();
                            initDistrctView();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                    }
                });
    }

    private void initDistrctView() {
        distictLayout.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return districtObjects.size();
            }

            @Override
            public Object getItem(int i) {
                return districtObjects.get(i);
            }

            @Override
            public long getItemId(int i) {
                return Long.valueOf(districtObjects.get(i).getCode());
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                if (view == null) {
                    view = mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_xiaofei_district_hot_item, null);
                }
                view.setTag(districtObjects.get(i));
                ((TextView) view).setText(districtObjects.get(i).getName());
                return view;
            }
        });
        distictLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                selectDistrictObject = (DistrictObject) view.getTag();
                cityLayout.setVisibility(View.GONE);
                city.setText(selectDistrictObject.getName());
                initListDate();
            }
        });
    }

    private void initCityListData() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("userId", App.sPreferences.getUserId());
        ServiceHelper.send(mBaseActivity, ServiceHelper.GET_CITYS, paramMap,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onCancelled() {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        CityObjectResponce rsp = JsonUtils.parseJsonStrToObject(responseInfo.result, CityObjectResponce.class);
                        if (rsp.getBody() != null) {
                            cityObjects.clear();
                            cityObjects.addAll(rsp.getBody());
                        }
                        initCityView();
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                    }
                });
    }


    private void initCityView() {
        String name = selectCityObject.getCityName();
        //如果有跟预设城市相同的，继续查找该城市区域
        if (StringUtil.isNotEmpty(App.sPreferences.getCityName())) {
            name = App.sPreferences.getCityName();
            selectCityObject = new CityObject(App.sPreferences.getCityName(), App.sPreferences.getCityCode());
            for (CityObject cityObject : cityObjects) {
                if (name.equalsIgnoreCase(cityObject.getCityName())) {
                    name = App.sPreferences.getCityName();
                    selectCityObject = cityObject;
                    break;
                }
            }
        }
        initCityEreaData();
        city.setText(name);
        currentCity.setText("所在城市：" + name);
    }



    public void OnResume() {
        super.onResume();
    }

    public static List<BusinessType> getBusinessTypes() {
        return businessTypes;
    }

    public static List<DistrictObject> getDistrictObjects() {
        return districtObjects;
    }

    /**
     * @return the homeFragmentInstent
     */
    public static HomeFragment getHomeFragmentInstent() {
        return homeFragmentInstent;
    }

}
